let mongoose = require('mongoose');

let newSkinCount=3;
let thumbtackCount=5;
let bombColorCount=5;

let schema = new mongoose.Schema({
    usedNewSkinCount:{ //已使用次数
        type:Number,
        default:0
    },
    tempNewSkinCount:{ //视频获取的新皮肤
        type:Number,
        default:0
    },
    usedThumbtackCount:{ //已使用次数
        type:Number,
        default:0
    },
    tempThumbtackCount:{ //分享临时获取的图钉数量
        type:Number,
        default:0
    },
    tempBombColor:{  //临时获取的泡泡颜色
        type:Number,
        default:0
    },
    usedBombColorCount:{ //已使用临时泡泡颜色次数
        type:Number,
        default:0
    },
    lastRefreshTick :{
        type:Number,
        default:0
    }
},{versionKey:false});

schema.methods={
    getMessage(options){
        this._refresh();
        let newSkinNumber=options.self.ppt.getNewSkin(options);
        let newBombColor=1+Math.floor(Math.random()*4);
        return {
            role:options.self.ppt.role,
            newSkinCount:newSkinCount,
            newBombColor:newBombColor,
            usedNewSkinCount:this.usedNewSkinCount,
            tempNewSkinCount:this.tempNewSkinCount,
            thumbtackCount:thumbtackCount,
            usedThumbtackCount:this.usedThumbtackCount,
            tempThumbtackCount:this.tempThumbtackCount,
            bagThumbtackCount:options.self.bag.getCount(101),
            bombColorCount:bombColorCount,
            tempBombColor:this.tempBombColor,
            usedBombColorCount:this.usedBombColorCount,
            newSkin:newSkinNumber,
            hasNewSkin:options.self.ppt.getSkins(options).indexOf(newSkinNumber)>=0,
            hasNewBombColor:options.self.ppt.bombColors.indexOf(newBombColor)>=0
        }
    },
    useNewSkin(){
        if(this.usedNewSkinCount===newSkinCount){
            throw new Error('你今天试用新皮肤的机会已经用完了。')
        }
        if(this.tempNewSkinCount===0){
            this.tempNewSkinCount=1;
        }
        this.usedNewSkinCount++;
    },
    useThumbtack(){
        if(this.usedThumbtackCount===thumbtackCount){
            throw new Error('你今天试用复活针的机会已经用完了。')
        }
        if(this.tempThumbtackCount===0){
            this.tempThumbtackCount=1;
        }
        this.usedThumbtackCount++;
    },
    useBombColor(options={color:0}){
        if(this.usedBombColorCount===bombColorCount){
            throw new Error('你今天试用多彩泡泡的机会已经用完了。')
        }
        if(this.tempBombColor===0){
            this.tempBombColor=parseInt(options.color);
        }
        this.usedBombColorCount++;
    },
    _refresh(){
        let now=new Date();
        let last=new Date(this.lastRefreshTick);
        if(now.toLocaleDateString()!==last.toLocaleDateString()){
            this.newSkinCount=3;
            this.usedNewSkinCount=0;
            this.thumbtackCount=3;
            this.usedThumbtackCount=0;
            this.tempBombColor=0;
            this.usedBombColorCount=0;
            this.lastRefreshTick=now.getTime();
        }
    }
};

module.exports = schema;